<template>
  <div>
    <el-form :model="formData" :rules="rules">
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="formData.mobile" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="password2">
        <el-input v-model="formData.password2" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  data() {
    const checkPassword2 = (rule, value, callback) => {
      // 1. 校验逻辑, 输出true / false
      const isSamePassword = value === this.formData.password
      // 2. 后续处理(饿了么专有写法)
      if (isSamePassword) {
        // 成功 直接调用 callback 即可
        callback()
      } else {
        // 失败 调用 callback 传入一个原生错误对象
        callback(new Error('两次密码必须一致'))
      }
    }

    return {
      formData: {
        mobile: '',
        password: '',
        password2: ''
      },
      rules: {
        mobile: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { trigger: 'blur', validator: checkPassword2 }
        ]
      }
    }
  }
}
</script>

<style>

</style>
